/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.era7bioinformatics.aws.s3.components.beta1

import com.era7bioinformatics.aws.s3.components.generic.data._

class BucketNameImpl(name: String) extends BucketName {

  override def toString: String = value

  lazy val value: String = name
  
}


object BucketNameImpl {

  import scala.util.matching.Regex

  private val hostNameRegexpString: String = """^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$"""

  private val hostDNSregex: Regex = hostNameRegexpString.r

  private def checkLength(wannabe: String): Boolean = (2 < wannabe.length) && (wannabe.length< 64)

  
  
  private def checkName(wannabe: String): Boolean = {   

    if(checkLength(wannabe)) {

      val matched: Option[String] = hostDNSregex.findFirstIn(wannabe)

      matched match {

          case Some(n) => n.equals(wannabe)
          case _ => false
      }

    } else false
    
  }

  def unapply(name: String): Option[BucketName] = {

    if(checkName(name))
      Some(new BucketNameImpl(name))
    else
      None
  }

  def apply(name: String): Option[BucketName] = unapply(name)


  
  
}